package com.itboye.jigongbao.activity

import android.net.Uri
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.itboye.jigongbao.R
import com.itboye.jigongbao.base.BaseActivity
import com.itboye.jigongbao.fragments.wagesbillsfragments.WagesBillsFragment
import com.itboye.jigongbao.utils.Const
import com.itboye.jigongbao.utils.ToastUtil.ByAlert

class WagesBillActivity : BaseActivity(),WagesBillsFragment.OnFragmentInteractionListener {
    override fun onFragmentInteraction(uri: Uri?) {
        ByAlert.alert("監聽")
    }

    var re_actionbar_wagesbill: RelativeLayout? = null
    var v_statusbar_wagesbill: View? = null
    var tabitem_1: TextView? = null
    var tabitem_2: TextView? = null
    var img_back: ImageView? = null

    var fragment1: WagesBillsFragment? = null
    var fragment2: WagesBillsFragment? = null

    var fragmentTransaction: FragmentTransaction? = null
    var fragmentManager: FragmentManager? = null
    override fun getLayoutId(): Int {

        return R.layout.activity_wages_bill
    }

    override fun initData() {
        fragmentManager = supportFragmentManager
        val params = v_statusbar_wagesbill!!.getLayoutParams()
        params.height = getStatusBarHeight()
        v_statusbar_wagesbill!!.setLayoutParams(params)
        v_statusbar_wagesbill!!.setBackgroundColor(resources.getColor(R.color.main_blue))
        changeTabSelect(0)
    }

    private fun changeTabSelect(index: Int) {
        Log.v(Const.Companion.TAG_DEBUG, "我是索引${index}")
        fragmentTransaction = fragmentManager!!.beginTransaction()
        hiddenFragment()
        when (index) {
            0 -> {
                //选中第一个tab
                tabitem_1!!.setBackgroundDrawable((resources.getDrawable(R.drawable.tab_left_select_style_bg_white)))
                tabitem_2!!.setBackgroundDrawable((resources.getDrawable(R.drawable.tab_right_select_style_bg_green)))
                tabitem_2!!.setTextColor(resources.getColor(R.color.white))
                tabitem_1!!.setTextColor(resources.getColor(R.color.main_blue))
                if (fragment1 == null) {
                    fragment1 = WagesBillsFragment.newInstance("$index", "")
                    fragmentTransaction!!.add(R.id.frameLayout_wagesbills, fragment1)
                } else {
                    fragmentTransaction!!.show(fragment1)
                }
            }
            1 -> {
                //选中第二个tab
                tabitem_1!!.setBackgroundDrawable((resources.getDrawable(R.drawable.tab_left_select_style_bg_green)))
                tabitem_2!!.setBackgroundDrawable((resources.getDrawable(R.drawable.tab_right_select_style_bg_white)))
                tabitem_1!!.setTextColor(resources.getColor(R.color.white))
                tabitem_2!!.setTextColor(resources.getColor(R.color.main_blue))
                if (fragment2 == null) {
                    fragment2 = WagesBillsFragment.newInstance("$index", "")
                    fragmentTransaction!!.add(R.id.frameLayout_wagesbills, fragment2)
                } else {
                    fragmentTransaction!!.show(fragment2)
                }
            }
            else -> {

            }
        }
        fragmentTransaction!!.commit()
    }

    private fun hiddenFragment() {

        if (fragment1 != null) {
            fragmentTransaction!!.hide(fragment1)
        }
        if (fragment2 != null) {
            fragmentTransaction!!.hide(fragment2)
        }
    }

    override fun onClick(p0: View?) {
        when (p0) {
            tabitem_1 -> {
                changeTabSelect(0)
            }
            tabitem_2 -> {
                changeTabSelect(1)
            }
            img_back -> {
                finish()
            }
            else -> {
            }
        }
    }

}
